﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using C = TrackerRealm.Jazz.Common.JazzConstants;
using TrackerRealm.Jazz.Client.Custom;

namespace TrackerRealm.Jazz.Client
{
    /// <summary>
    /// 
    /// </summary>
    public class LoggedinUser: IDisposable
    {
        ClientNexusBase nexus;
        #region Constructors
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="loginUser"></param>
        /// <param name="nexus"></param>
        public LoggedinUser(jProfileBase loginUser, ClientNexusBase nexus)
        {
            this.nexus = nexus;
            this.User = loginUser;
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public jProfileBase User
        {
            get;
            private set;
        }
        
        #region IDisposable Members
        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            jUser user;
            // should be moved to nexus - logoff command?
            if (!this.nexus.Cache.NamedObjects.TryGetSingleUnit<jUser>(C.GUEST, out user))
            {
                user = jUser.Create(C.GUEST);
                nexus.Bind(user);
            }
            nexus.ChangeLoginProfile(user);
        }

        #endregion
    }
}
